<template>
	<!-- 常用语 -->
	<view>
		<baseHead :title="title" :showReturn="true" text_right="添加" :right="true" @clickRight="clickRight"></baseHead>
		<view class="box">
			<view v-if="datalist.length>0">
				<view class="contentBox" v-for="(item,index) in datalist">
					<view class="content">
						<view>{{ item.Content }}</view>
					</view>
					<view class="contentbut">
						<view>
							<text @click="bottomBut(item,index)" v-show="item.Type != 1">删除</text>
							<text v-show="item.Type == 1">--</text>
						</view>
						<view>
							<text @click="dataclick(item)" v-show="item.Type != 1">编辑</text>
							<text v-show="item.Type == 1">--</text>
						</view>
						<view>
							<u-checkbox-group placement="column" @change="checkboxChange(item,index)">
								<u-checkbox :customStyle="{marginBottom: '8px'}" :checked="item.IsDefault" size='13'
									label="默认">
								</u-checkbox>
							</u-checkbox-group>
						</view>
					</view>
				</view>
				<u-loadmore :status="status" />
			</view>

			<view v-else>
				<u-empty mode="data" icon="/static/recruit/index/qsy.png" width="350" marginTop="150">
				</u-empty>
			</view>
		</view>

		<u-popup :show="show" @close="close" :mode="mode" :round="10">
			<view style="padding: 50rpx 20rpx; width: 550rpx">
				<view style="text-align: center">
					<text>常用语</text>
				</view>
				<view style="margin: 20rpx 0rpx">
					<u--textarea v-model="value1" placeholder="请输入内容" count maxlength="40"></u--textarea>
				</view>
				<view class="fl-lr-c">
					<text class="but1" @click="close"> 取消 </text>
					<text class="but2" @click="add"> 确定 </text>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import baseHead from "@/pages/component/baseHead.vue";
	export default {
		data() {
			return {
				title: "常用语",
				show: false,
				mode: "center",
				value1: "",
				PageIndex: 1,
				PageSize: 20,

				datalist: [],
				status: "loadmore",
			};
		},
		components: {
			baseHead,
		},
		onReachBottom() {
			// 加载前值为loadmore，加载中为loading，没有数据为nomore
			if (this.status != "nomore") {
				this.status = "loading";
				this.listdata()
			}
		},
		onShow() {
			this.datalist = [];
			this.lsitdata();
		},
		methods: {
			checkboxChange(item, ind) {
				console.log(item, ind);

				let obj = {
					Id: item.Id,
					IsDefault: true,
				}
				console.log(obj);
				this.$http.core.phrasescorpsetdefaultfunction(obj).then(res6 => {
					if (res6.Success) {
						// this.$util.toast("设置默认成功");


						this.datalist.forEach((res, index) => {
							if (ind == index) {
								res.IsDefault = !item.IsDefault;
							} else {
								res.IsDefault = false;
							}
						})

					}
				})
			},
			lsitdata() {
				this.$http.core.phrases_corplist({
					PageIndex: this.PageIndex,
					PageSize: this.PageSize
				}).then((res) => {
					console.log(res);
					if (res.Result) {
						if (res.Result.Items.length == 0) {
							// 没有数据----
							this.status = "nomore";
							return;
						}
						this.datalist = this.datalist.concat(res.Result.Items);
						console.log(res.Result.Items.length, this.PageSize)
						if (res.Result.Items.length >= this.PageSize) {
							this.PageIndex++;
							// 表示还有数据
						} else {
							//没有更多数据了
							this.status = "nomore";
						}
					}
				});
			},
			add() {
				if (!this.value1) {
					this.$util.toast("请输入");
					return;
				}

				let obj = {
					Content: this.value1,
				};

				this.$http.core.phrases_corpadd(obj).then((res) => {
					console.log(res);
					if (res.Success) {
						this.$util.toast("添加成功");
						this.show = false;
						this.value1 = "";
						this.PageIndex = 1;
						this.PageSize = 20;
						this.datalist = [];
						this.status = "loadmore";
						this.lsitdata();
					}
				});
			},

			dataclick(item) {
				console.log(item);

				// if (item.Type == 1) {
				// 	return
				// }

				uni.navigateTo({
					//url:"pages/views/packageB/type-jobhunting/jibenshezhi/jibenziliao/changyongyuxiangqing"
					url: "./changyongyuxiangqing?time=" + JSON.stringify(item),
				});
			},
			bottomBut(item, delete_index) {
				console.log(item);
				let that = this
				uni.showModal({
					title: "提示",
					content: "确认删除吗",
					success: function(res) {
						if (res.confirm) {

							let obj = {
								Ids: [item.Id],
							};
							that.$http.core.phrases_corpdel(obj).then((res) => {
								if (res.Success) {
									that.$util.toast("删除成功");
									// this.status = "loadmore";
									// this.datalist.splice(delete_index, 1)

									that.PageIndex = 1;
									that.PageSize = 20;
									that.datalist = [];
									that.status = 'loadmore';
									that.lsitdata();

								}
							})
						}
					},

				})

			},
			close() {
				this.show = false;
			},
			clickRight() {
				this.show = true;
			}
		}
	}
</script>

<style scoped lang="scss">
	.box {
		box-sizing: border-box;

		.contentBox {
			border-radius: 8rpx;
			background: #ffffff;
			padding: 10rpx 30rpx;
			margin: 20rpx 0;

			.content {
				// margin-top: 1rpx;
				padding: 20rpx 0rpx;
				font-size: 28rpx;
				line-height: 36rpx;
				border-bottom: 2rpx solid #f1f2f6;
			}

			.contentbut {
				display: flex;
				justify-content: space-between;
				padding: 20rpx;
				font-size: 26rpx;

				.textbut {}
			}
		}
	}

	.but1 {
		border: 1rpx solid #a0a0a0;
		padding: 10rpx 40rpx;
		color: #a0a0a0;
		border-radius: 10rpx;
	}

	.but2 {
		padding: 10rpx 40rpx;
		border-radius: 10rpx;
		background-color: #1684fc;
		color: #ffffff;
	}
</style>